package cst.neu.yuejian_31.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.activity.OrderDetailActivity;
import cst.neu.yuejian_31.bean.TOrderBean;
import cst.neu.yuejian_31.Common.Constant;

/**
 * Created by xxe on 2017/10/30.
 */

public class TOrderAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<TOrderBean> list;
    private String state;

    public TOrderAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<TOrderBean> list){
        this.list=list;
        notifyDataSetChanged();

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView orderId,orderName,orderMoney,orderNum,orderTotal,orderTime;
        private ImageView orderPic,orderState;
        public ViewHolder(View itemView) {
            super(itemView);
            orderId= (TextView) itemView.findViewById(R.id.textView_order_id);
            orderName= (TextView) itemView.findViewById(R.id.textView_oname);
            orderMoney= (TextView) itemView.findViewById(R.id.textView_omoney);
            orderNum= (TextView) itemView.findViewById(R.id.textView_onum);
            orderTotal= (TextView) itemView.findViewById(R.id.textView_ototal);
            orderPic= (ImageView) itemView.findViewById(R.id.imageView_order_pic);
            orderState= (ImageView) itemView.findViewById(R.id.imageView_order_state);
            orderTime= (TextView) itemView.findViewById(R.id.textView_order_time);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item_order,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final TOrderBean tOrderBean=list.get(position);
        if (tOrderBean==null)
            return;
        ViewHolder viewHolder= (ViewHolder) holder;
        state=tOrderBean.getState();
        viewHolder.orderId.setText(tOrderBean.getOrder_id());
        viewHolder.orderNum.setText(tOrderBean.getNum());
        viewHolder.orderName.setText(tOrderBean.getTrain_name());
        viewHolder.orderMoney.setText(tOrderBean.getTrain_price());
        viewHolder.orderTotal.setText(tOrderBean.getTotal());
        viewHolder.orderTime.setText(tOrderBean.getOrder_time());
        Picasso.with(context).load(Constant.TRAIN_URL+tOrderBean.getTrain_pic().toString()).into(viewHolder.orderPic);
//        STATE=String.valueOf(state);
        if (state.equals("1")){
            viewHolder.orderState.setImageResource(R.drawable.ic_finish);
        }else {
            viewHolder.orderState.setImageResource(R.drawable.ic_unfinished);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, OrderDetailActivity.class);
                intent.putExtra("order_id",tOrderBean.getOrder_id());
                intent.putExtra("order_num",tOrderBean.getNum());
                intent.putExtra("order_name",tOrderBean.getTrain_name());
                intent.putExtra("order_money",tOrderBean.getTrain_price());
                intent.putExtra("order_total",tOrderBean.getTotal());
                intent.putExtra("order_time",tOrderBean.getOrder_time());
                intent.putExtra("order_intro",tOrderBean.getTrain_intro());
                intent.putExtra("order_tpic",tOrderBean.getTrain_pic());
                intent.putExtra("order_state",tOrderBean.getState());
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        if (list==null)return 0;
        else
            return list.size();
    }
}
